package com.motong.gongdan.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.motong.gongdan.entity.LampRequestNotice;
import com.motong.gongdan.entity.LampType;
import com.motong.gongdan.entity.LampTypeUser;
import com.motong.gongdan.mapper.LampRequestNoticeMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LampRequestNoticeService extends ServiceImpl<LampRequestNoticeMapper, LampRequestNotice> {

    @Resource
    private LampTypeUserService lampTypeUserService;

    @Resource
    private LampTypeService lampTypeService;


    public void saveByLampTypeId(Long lampTypeId, Long lampRequestId, String deviceCode) {
        LampType lampType = lampTypeService.getById(lampTypeId);
        String noticeText = "设备".concat(deviceCode).concat("发起了").concat(lampType.getName()).concat("联络灯呼叫,请及时处理");
        this.remove(new LambdaQueryWrapper<LampRequestNotice>().eq(LampRequestNotice::getLampRequestId, lampRequestId));
        List<LampTypeUser> lampTypeUsers = lampTypeUserService.listByLampTypeId(lampType.getId());
        Date currentDate = new Date();
        if (!CollectionUtils.isEmpty(lampTypeUsers)) {
            this.noticeUserByLampUsers(noticeText,currentDate,lampRequestId,lampTypeUsers);
        }
    }


    public void noticeUserByLampUsers(String noticeText,Date currentDate, Long lampRequestId, List<LampTypeUser> lampTypeUsers) {
        List<LampRequestNotice> lampRequestNotices = new ArrayList<>();
        lampTypeUsers.forEach(i -> {
            DateTime noticeTime = DateUtil.offsetMinute(currentDate, i.getDelayTime());
            //微信通知
            if (i.getInfoType().contains("0")) {
                LampRequestNotice lampRequestWX = new LampRequestNotice().setLampRequestId(lampRequestId).setInfoStatus(0).setUserId(i.getUserId())
                        .setRoundNum(i.getRoundNum()).setIntervalTime(i.getIntervalTime())
                        .setNoticeText(noticeText).setNoticeTime(noticeTime).setInfoType(0).setCreateTime(currentDate);
                lampRequestNotices.add(lampRequestWX);
            }
            // ai语音通知
            if (i.getInfoType().contains("1")) {
                LampRequestNotice lampRequestAI = new LampRequestNotice().setLampRequestId(lampRequestId).setInfoStatus(0).setUserId(i.getUserId())
                        .setRoundNum(i.getRoundNum()).setIntervalTime(i.getIntervalTime())
                        .setNoticeText(noticeText).setNoticeTime(noticeTime).setInfoType(1).setCreateTime(currentDate);
                lampRequestNotices.add(lampRequestAI);
            }

        });
        if (!CollectionUtils.isEmpty(lampRequestNotices)) {
            this.saveBatch(lampRequestNotices);
        }
    }
}
